// ml278
// Copyright (C) 2008 Bjørn Øivind Bjørnsen, Aleksander Gjermundsen, Knut Andre Tidemann, Thomas Hansen
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#include <stdio.h>
#include <string>
#include <iostream>
#include <cstdlib>

int main(int argc, char *argv[])
{
	unsigned int PageSize = 1;
	unsigned int TileSize = 65;
	unsigned int ZoneSize = 65;
	unsigned int pagerow = PageSize * (TileSize -  1) + 1;

	if(argc < 2)
	{
		std::cout << "Usage: " << argv[0] << " <filename> [<ZoneSize> <PageSize> <TileSize>]" << std::endl;
		return 0;
	}

	if(argc > 2 && argc < 5)
	{
		std::cout << "Usage: " << argv[0] << " <filename> [<ZoneSize> <PageSize> <TileSize>]" << std::endl;
		return 0;
	}

	if(argc > 2)
	{
		ZoneSize = atoi(argv[2]);
		PageSize = atoi(argv[3]);
		TileSize = atoi(argv[4]);
		pagerow = PageSize * (TileSize -  1) + 1;
	}


	unsigned short nullBuffer = 32768;
	//unsigned char nullBuffer[2] = { 0x80, 0x00 };

	std::string filename = argv[1];

	for(int i = 1; i <= ZoneSize * ZoneSize; i++)
	{
		char nr[6] = { 0 };
		sprintf(nr,"_%04d",i);
		std::string fn = filename + nr;

		std::cout << "Generating " << fn << " with " << pagerow * pagerow  << " vertices" << std::endl;

		FILE *fp = fopen(fn.c_str(),"w+");

		if(fp == NULL)
		{
			std::cout << "ERROR: Cannot open file: " << fn << std::endl;
			return 0;
		}

		for(int j = 0; j < pagerow * pagerow; j++)
			fwrite(&nullBuffer,sizeof(unsigned short),1,fp);
		fclose(fp);
	}

	return 0;
}

